// TypeScript file
class SceneManages {
    private static isAvailable = false;
    private static obj;
    public static getInstance(baba){
        if(!SceneManages.obj){
            SceneManages.obj = new SceneManages(baba);
            SceneManages.obj.isAvailable = true;
        }
        return SceneManages.obj;
    }
    private _baba;
    public constructor(baba){
        this._baba = baba;
        if (!SceneManages.isAvailable) {
			console.log(this);
			console.warn("你确定要用new关键字？单例模式必须调用类的getInstanc方法来实例化对像");
		}
    }

    private _groupName;
    private _lastSence;
    private _nextSence;
    private _callback;
    private _loading;
    public loadSence(groupName:string,senceName:egret.DisplayObjectContainer,callback=function(){}){
        this._loading = new LoadingUI();
        this._baba.addChild(this._loading);
        this._nextSence = senceName;
        this._groupName = groupName;
        this._callback = callback;
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onGroupLoadCompleted, this);
		RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
		RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
		RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
		RES.loadGroup(groupName);
    }
    //资源组加载出错
	private onResourceLoadError(event: RES.ResourceEvent) {
		console.warn("Group:" + event.groupName + " has failed to load");
		//忽略加载失败的项目
		this.onGroupLoadCompleted(event);
	}
	// preload资源组加载进度
	private onResourceProgress(event: RES.ResourceEvent) {
		if (event.groupName == this._groupName) {
			this._loading.setProgress(event.itemsLoaded, event.itemsTotal);
		}
	}
	//资源组加载出错
	private onItemLoadError(event: RES.ResourceEvent) {
		console.warn("Url:" + event.resItem.url + " has failed to load");
	}
	//资源组加载成功
    private onGroupLoadCompleted(e){
        if(e.groupName == this._groupName){
            //移除监听事件
			RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onGroupLoadCompleted, this);
			RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
			RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
			RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
            //移除进度条
            this._baba.removeChild(this._loading);
            let nextSence = new this._nextSence();
            this._baba.addChild(nextSence);
            nextSence.anchorOffsetX = nextSence.width >> 1;
			nextSence.anchorOffsetY = nextSence.height >> 1;
			nextSence.x = this._baba.stageWidth>>1 ;
			nextSence.y = this._baba.stageHeight>>1 ;
			nextSence.scaleX = 0;
			nextSence.scaleY = 0;
            egret.Tween.get(nextSence).to({ scaleX:1,scaleY:1 }, 500).call(function () {
                if(this._lastSence){
                    this._baba.removeChild(this._lastSence);
                }
                this._lastSence = nextSence;
                this._callback();
            }.bind(this));
        }
    }
}